{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>

<form class="layui-form" action="" id="cross_srh">
    <div class="proxy_srh">
        <div class="layui-inline">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label class="layui-form-label">project_name：</label>
                    <div class="layui-input-inline">
                        <select class="layui-select" name="project_name" id="project_name" lay-verify="" lay-search
                                lay-filter="project_name">

                            {% for d in projectAll %}
                                <option value={{d.id}}>{{d.project_name}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="layui-input-inline">
                    <label class="layui-form-label">module_name：</label>
                    <div class="layui-input-inline" id="module_div">
                       <select class="layui-select" name="module_name" id="module_name" lay-verify="" lay-search
                                lay-filter="module_name">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">plan_name：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="api_plan_name" id="api_plan_name" placeholder="请输入" style="width:212px">
                    </div>
                </div>

                <div class="layui-inline" >
                    <button class="layui-btn" type="button" id="start" lay-submit lay-filter="start">查询</button>
                </div>
            </div>
        </div>

    </div>
</form>
<hr class="layui-bg-gray">
<button class="layui-btn  layui-btn-normal" id="add" type="button">
    <i class="layui-icon"></i> 新建任务
</button>

<script type="text/html" id="filemange_v1">
    <button type="button"  class="layui-btn layui-btn-xs " lay-event="update">更新</button>
    <button type="button"  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="start">启用</button>
    <button type="button"  class="layui-btn layui-btn-xs " lay-event="stop">停止</button>
    <button type="button"  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="runNow">执行</button>
</script>

<table class="layui-hide" id="apiCase" lay-filter="apiCase"></table>

<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var loading = layer.load();

          var  options = {
                elem:'#apiCase',
                skin:"line",
                page:true,
                height: 'full-200',
                toolbar: true,
                url:'/case/api/plan/srh?',
                limit:10,
                limits:[10,20,30],
                where:{
                    project_id : $('#project_name option:selected').val(),
                    module_id: $('#module_name option:selected').val(),
                    api_plan_name: $('#api_plan_name').val(),

                },
                response:{
                    statusName:'errorCode',
                    statusCode:'',
                    dataName:'data'
                },
                cols:[[
                       {type: 'numbers'}
                      ,{field: 'api_plan_name', title: 'plan_name',sort:true}
                      ,{field: 'auto_order', title: 'auto_order',sort:true}
                      ,{field: 'caseIdList', title: 'caseIdList',sort:true,width:1}
                      ,{field: 'id', title: 'id',sort:true,width:1}
                      ,{field: 'isrun', title: 'isRun',sort:true,templet: function(d){
                            if(d.isrun==0){
                                return '<span style="color:red">运行中</span>'
                            }else{
                                return '<span style="color:green">停止</span>'
                            }
                      }}
                      ,{field: 'last_time', title: 'last_time',sort:true}

                      ,{fixed: 'right', title:'', toolbar: '#filemange_v1',width:220}

                ]],
                id:'apiCase',
                done:function(res,cur,count){
                    $("[data-field='caseIdList']").css('display','none');
                    $("[data-field='id']").css('display','none');
                    layer.close(loading);
                }
              };
          var tableIns = table.render(options);

          table.on('row(apiCase)', function(obj){
                obj.tr.css("background","#eeeeee");
          });
          form.on('select(project_name)', function(obj){
                $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/moudle/all/select?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:{"project_id":obj.value,"moudle_type":"1"},
                        success: function(result){
                            s1 = '<div class="layui-input-inline"> <select class="layui-select" name="module_name" id="module_name" lay-verify="" lay-search lay-filter="module_name"><option value="">请选择</option>';
                            e1 = '</select></div>';
                             if(result.data.length>0){

                                str = '';
                                layui.each(result.data,function(k,v){
                                        str = str+'<option value="'+v.id+'">'+v.moudle_name+'</option>'
                                });
                                htmlDiv = s1+str+e1;
                                $("#module_div").html(htmlDiv);


                             }else{
                                $("#module_div").html(s1+e1);
                             }
                             form.render();


                       }
                    });
                  form.render('select');
          });
          table.on('rowDouble(apiCase)', function(obj){
            if(obj.data.caseIdList == ""||obj.data.caseIdList==undefined){
                return false;
            }
            if($(".layui-layer-close").length == 0 ){
                        divRunContent = '<div class="layui-inline"><pre id="json"></pre></div>';
                        index = layer.open({
                                    type:1,
                                    skin: 'demo-class',
                                    content:divRunContent,
                                    area:['666px','346px'],
                                    offset:  'lb',
                                    title:obj.data.api_plan_name + "——运行",
                                    closeBtn:1,
                                    shade: 0,
                                    id:"runApiCase",
                                    shadeClose:true,
                                    maxmin: true,
                                    success: function(index, layero){

                                    }
                        });
            }
                $('#json').jsonViewer(obj.data.caseIdList);

          });
          $('#add').on('click',function(){
                layui.data('test', {key: 'apiPlanCache',value: undefined});
                index=layer.open({
                                                type:2,
                                                content:"/case/plan/manage?token={{token}}&openId={{openId}}",
                                                area:['425px','527px'],
                                                //offset: ['231px','307px'],
                                                title:"任务管理",
                                                closeBtn:1,
                                                shade: 0,
                                                shadeClose:true,
                                                //maxmin: true,
                                                cancel: function(index, layero){
                                                     $('#start').click();
                                                }
                });

          });


          //更新或者删除案例
          table.on('tool(apiCase)', function(obj){
                if(obj.event === 'update'){
                    layui.data('test', {key: 'apiPlanCache',value: obj.data});
                    index=layer.open({
                                                type:2,
                                                content:"/case/plan/manage?token={{token}}&openId={{openId}}",
                                                area:['425px','527px'],
                                                //offset: ['231px','307px'],
                                                title:"任务管理",
                                                closeBtn:1,
                                                shade: 0,
                                                shadeClose:true,
                                                //maxmin: true,
                                                cancel: function(index, layero){
                                                     $('#start').click();
                                                }
                    });
                }else if(obj.event === 'stop'){
                    layer.confirm('真的要停止么?', function(index){
                        $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/case/api/plan/del",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify({"id":obj.data.id,"isRun":1}),
                                    success: function(result){
                                        obj.update({isrun:1,});
                                        layer.close(index);
                                    }
                        });
                    });
                }else if(obj.event === 'start'){
                    layer.confirm('真的要启用么?', function(index){
                        $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/case/api/plan/del",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify({"id":obj.data.id,"isRun":0}),
                                    success: function(result){
                                        obj.update({isrun:0,});
                                        layer.close(index);
                                    }
                        });
                    });
                }
          });


          //点击查询
          $('#start').on('click',function(){
              var loading = layer.load();

              options = {
                elem:'#apiCase',
                skin:"line",
                page:true,
                height: 'full-200',
                toolbar: true,
                url:'/case/api/plan/srh?',
                limit:10,
                limits:[10,20,30],
                where:{
                    project_id : $('#project_name option:selected').val(),
                    module_id: $('#module_name option:selected').val(),
                    api_plan_name: $('#api_plan_name').val(),

                }
              }
              tableIns.reload(options);
              layer.close(loading);
          });




          form.render();
          layer.close(loading);

    });
</script>
{% endblock %}